package com.eos.control.action.emp;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.eos.common.tool.DataCacheUtil;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.SysMenu;
import com.eos.orm.mybatis.model.SysRole;
import com.eos.orm.mybatis.model.ViewRoleMenu;

public class EditRoleAction extends AbstractBaseAction {
	private static final long serialVersionUID = 874115284318087039L;
	private Integer roleId;
	private SysRole role;
	private String menuArr;
	
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}
	
	public SysRole getRole() {
		return role;
	}

	public String getMenuArr() {
		return menuArr;
	}

	@Override
	public String execute()  {
		 try{
			 role = DataCacheUtil.getSysRole(roleId);
			 List<SysMenu> menuList = DataCacheUtil.getAllSysMenu();
			 List<ViewRoleMenu> roleMenuList = DataCacheUtil.getSysMenuOfRole(roleId);
			 JSONArray jsonArr = new JSONArray();
			 int index = 0;
			 int check = 0;
			for(SysMenu sm : menuList){
				JSONObject obj = new JSONObject(ObjectUtils.beanToJson(sm));
				for(ViewRoleMenu vrm : roleMenuList){
					 if(vrm.getMenuId().intValue() == sm.getId().intValue()){
						check = 1;
						break;
					 } 
				 }
				obj.put("check", check);
				check = 0;
				jsonArr.put(index++, obj);
			 }
			 menuArr = jsonArr.toString();
		 }catch(Exception ex){
			 ex.printStackTrace();
		 }
		 return SUCCESS;
	}
}
